package com.kuanrf.upload.util;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.UUID;

import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.ServletContext;

import com.kuanrf.common.tools.FileTools;
import com.kuanrf.common.tools.ImageCompressTools;
import com.sun.imageio.plugins.bmp.BMPImageReader;
import com.sun.imageio.plugins.gif.GIFImageReader;
import com.sun.imageio.plugins.jpeg.JPEGImageReader;
import com.sun.imageio.plugins.png.PNGImageReader;

public class KinderTools {
	 /**
     * 检查文件类型
     * <功能详细描述>
     * @param paramFile
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String checkFile(File paramFile)
    {
        if ((paramFile == null) || (paramFile.length() == 0L))
            return null;
        try
        {
            String str = null;
            ImageInputStream localImageInputStream = ImageIO.createImageInputStream(paramFile);
            Iterator localIterator = ImageIO.getImageReaders(localImageInputStream);
            if (!(localIterator.hasNext()))
                return null;
            ImageReader localImageReader = (ImageReader) localIterator.next();
            if (localImageReader instanceof JPEGImageReader)
                str = "jpg";
            else if (localImageReader instanceof GIFImageReader)
                str = "gif";
            else if (localImageReader instanceof BMPImageReader)
                str = "bmp";
            else if (localImageReader instanceof PNGImageReader)
                str = "png";
            localImageInputStream.close();
            return str;
        }
        catch (IOException localIOException)
        {
            localIOException.printStackTrace();
        }
        return null;
    }
    
    /**
     * 图片URL地址
     * <功能详细描述>
     * @param paramServletContext
     * @param paramFile
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String fileUrl(ServletContext paramServletContext,
            String path, String url, File paramFile, String kinderType)
    {
        if (paramFile == null)
        {
            return null;
        }
        if (url == null)
        {
            return null;
        }
        if (!url.endsWith("/"))
        {
            url = url + "/";
        }
        String str1 = checkFile(paramFile);
        if (str1 == null)
        {
            throw new IllegalArgumentException("imageFile format error!");
        }
        String uuid = getUUID();
        //原图
        String str2 = path + "/" + uuid + "." + str1;
        //压缩过后的图片
        String str3 = path + "/" + uuid + "-touch." + str1;
        //原图
        File localFile1 = new File(str2);
        //压缩过后的图片
        File localFile3 = new File(str3);
        File localFile2 = localFile1.getParentFile();
        if (!(localFile2.isDirectory()))
            localFile2.mkdirs();
        try
        {
            if ("1".equals(kinderType))
            {
                //压缩图片
                ImageCompressTools.compressImage(paramFile,
                        localFile3,
                        320.0,
                        0,
                        true,
                        false);
                //复制原图
                FileTools.copyFile(paramFile, localFile1);
            }
            else
            {
                FileTools.copyFile(paramFile, localFile1);
            }
        }
        catch (Exception e)
        {
            
        }
        return url + uuid + "." + str1;
    }
    
    /**
     * UUID
     * <功能详细描述>
     * @return [参数说明]
     * 
     * @return String [返回类型说明]
     * @exception throws [违例类型] [违例说明]
     * @see [类、类#方法、类#成员]
     */
    public static String getUUID()
    {
        String str = UUID.randomUUID().toString();
        return str.substring(0, 8) + str.substring(9, 13)
                + str.substring(14, 18) + str.substring(19, 23)
                + str.substring(24);
    }
}
